﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Chaos.messages
{
    public class request_change_powersheet : message
    {
        stream.streamable powersheet = new model.powersheet();
        public model.powersheet Powersheet
        {
            get { return powersheet as model.powersheet; }
            set { powersheet = value; }
        }

        public override void process(context in_context)
        {
            ChangePowerSheetEvent e = new ChangePowerSheetEvent();
            e.Powersheet = Powersheet;
            System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                new OnChangePowerSheetDelegate(OnChangePowerSheetUIThread), e);    
        }

        public override void serialize(stream.stream in_Stream)
        {
            in_Stream.begin("request_change_powersheet")
                .serialize("powersheet", ref powersheet)
            .end();
        }


        public class ChangePowerSheetEvent : EventArgs
        {
            public model.powersheet Powersheet { get; set; }
            public ChangePowerSheetEvent()
            {
            }
        };
        public delegate void OnChangePowerSheetDelegate(ChangePowerSheetEvent e);


        public void OnChangePowerSheetUIThread(ChangePowerSheetEvent e)
        {
            network.gameserver s = System.Windows.Application.Current.FindResource("game_server") as network.gameserver;
            if (s != null)
            {
                model.game g = System.Windows.Application.Current.FindResource("model_game") as model.game;
                if (g != null)
                {
                    model.powersheet powersheet = g.Board.findpowersheetbyfaction(Powersheet.Faction);
                    if (powersheet != null)
                    {
                        powersheet.Power = Powersheet.Power;
                        process_model_game message = new process_model_game();
                        message.Game = g;
                        s.BroadcastMessage(message);
                    }
                }
            }
        }


    }
}
